package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j //日志
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);


        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        //在这里获取用户id，使用util工具类

        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        //builder方法也可以为对象属性赋值
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    //新增员工
    @PostMapping
    public Result<String> save(@RequestBody EmployeeDTO employeeDTO) {

        log.info("新增员工");
        employeeService.save(employeeDTO);
        return Result.success();

    }
    @GetMapping("/page")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO) {

        PageResult page = employeeService.page(employeePageQueryDTO);
//        return PageResult
        return Result.success(page);
    }

    //启用警用员工.id通过query表格传来.非查询类可以不用泛型
    @PostMapping("/status/{status}")
    //命名一致则不需要加（）说明是哪个参数--status
    public Result updateStatus(@PathVariable("status") Integer status,long id) {
        //1表示启用
        log.info(id+"号员工的状态即将被修改");
        employeeService.updateStatus(status,id);
        return Result.success();// data和msg非必须
    }

    //编辑员工  --分两步（查询回显，修改）
    @GetMapping("/{id}")
    public Result<Employee> get(@PathVariable Long id) {
        log.info("查询员工并回显"+id+"beichaxun");
        Employee employee= employeeService.select(id);
        return Result.success(employee);
    }

    //第二步
    //POST 一般用于“创建”操作，幂等性较弱（多次提交会生成多个资源）。
    //PUT 一般用于“更新”操作，幂等性较强（多次提交结果一样）。
    @PutMapping
    public Result update(@RequestBody EmployeeDTO employeedto) {
        employeeService.update(employeedto);
        return Result.success();
    }
}
